package com.suns.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.suns.controller.Code;
import com.suns.dao.IncomeDao;
import com.suns.dao.UserDao;
import com.suns.domain.Income;
import com.suns.domain.User;
import com.suns.exception.BusinessException;
import com.suns.service.IncomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

@Service
public class IncomeServiceImpl implements IncomeService {
    @Autowired
    IncomeDao incomeDao;
    @Autowired
    UserDao userDao;

    @Override
    public PageInfo<Income> getPage_C_Income(int i, int pageSize, String keywords) {
        // 使用 PageHelper 进行分页操作
        PageHelper.startPage(i, pageSize);
        LambdaQueryWrapper<Income> wrapper = new LambdaQueryWrapper<Income>();
        wrapper.eq(Income::getUsertype,"次卡");
        if(!Objects.equals(keywords, "null")){
            String likeKeywords = "%" + keywords.toLowerCase() + "%";
            wrapper.and(w -> w.like(Income::getId,likeKeywords)
                    .or().like(Income::getName,likeKeywords)
                    .or().like(Income::getMoney,likeKeywords)
                    .or().like(Income::getTel,likeKeywords)
                    .or().like(Income::getTime,likeKeywords)
                    .or().like(Income::getUsertype,likeKeywords)
            );
        }
        List<Income> incomeList = incomeDao.selectList(wrapper);
        return new PageInfo<>(incomeList);
    }


    @Override
    public PageInfo<Income> getPage_Y_Income(int i, int pageSize, String keywords) {
        // 使用 PageHelper 进行分页操作
        PageHelper.startPage(i, pageSize);
        LambdaQueryWrapper<Income> wrapper = new LambdaQueryWrapper<Income>();
        wrapper.eq(Income::getUsertype,"月卡");
        if(!Objects.equals(keywords, "null")){
            String likeKeywords = "%" + keywords.toLowerCase() + "%";
            wrapper.and(w -> w.like(Income::getId,likeKeywords)
                    .or().like(Income::getName,likeKeywords)
                    .or().like(Income::getMoney,likeKeywords)
                    .or().like(Income::getTel,likeKeywords)
                    .or().like(Income::getTime,likeKeywords)
                    .or().like(Income::getUsertype,likeKeywords)
            );
        }
        List<Income> incomeList = incomeDao.selectList(wrapper);
        return new PageInfo<>(incomeList);
    }

    @Override
    public BigDecimal get_Income(String usertype, String day, String month) {
        LambdaQueryWrapper<Income> wrapper = new LambdaQueryWrapper<Income>();
        wrapper.eq(Income::getUsertype,usertype);
        if(!Objects.equals(day, "null")){
            wrapper.and(w->w.apply("DATE_FORMAT(`time`, '%Y-%m-%d') = DATE_FORMAT({0}, '%Y-%m-%d')", day));
            return incomeDao.selectList(wrapper)
                    .stream().map(Income::getMoney)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
        }else {
            String likeMonth = "%" + month.toLowerCase() + "%";
            wrapper.and(w->w.like(Income::getTime,likeMonth));
            return incomeDao.selectList(wrapper)
                    .stream().map(Income::getMoney)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addIncome(String tel, String name, String userType, String time, BigDecimal oldmoney, BigDecimal remoney) {
        Income income=new Income();
        income.setName(name);
        income.setTel(tel);
        income.setTime(time);
        income.setUsertype(userType);
        income.setMoney(remoney);
        int a=incomeDao.insert(income);
        if(a<=0){
            throw new BusinessException(Code.UPDATE_ERR,"充值失败!");
        }
        LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(User::getTel,tel)
                .set(User::getMoney,oldmoney.add(remoney));
        return userDao.update(null,wrapper)==1;
    }
}
